package com.fingard.dsp.bank.directbank.psbc11;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IAcceptNotice;
import com.fingard.dsp.bank.directbank.psbc11.util.FileTransCmd;
import com.fingard.dsp.bank.directbank.psbc11.util.NcpaiFtpClient;
import com.fingard.generalobj.NoticeReq;
import com.fingard.generalobj.NoticeResp;
import com.fingard.net.listener.SocketReceiver;

import java.io.File;
import java.util.Date;

public class PSBC11AcceptNotice extends PSBC11Base implements IAcceptNotice {
	@Override
	public NoticeResp dealNotice(NoticeReq pNtReq) throws Exception {
		NcpaiFtpClient ftpClient = null;
    	NoticeResp resp = new NoticeResp();
    	String p_req = pNtReq.getReqString();
    	WriteBankLogLn("接收到请求：" + pNtReq.getFullString());
		try {
			p_req = p_req.substring(16);
			
			String[] tmpNotifyArray = p_req.split("\\|");
			String tmpDownFileName = tmpNotifyArray[4];
			WriteBankLogLn("待下载批次结果文件名:" + tmpDownFileName);

			ftpClient = new NcpaiFtpClient();
			FileTransCmd tmpFileCommand = new FileTransCmd();
			tmpFileCommand.setVs_outsys_code("9940001"); /* 请求接收系统代码，固定 */
			tmpFileCommand.setVs_req_flag("0"); /* 请求标志，固定 */
			tmpFileCommand.setVs_send_recv_flag("1"); /* 向保险系统接收文件标志，固定 */
			tmpFileCommand.setVs_local_file_dir("insu/" + bankFront.corpNo); /* 请求保险系统文件目录，insu固定，0001为各保险公司代码 */
			tmpFileCommand.setVs_local_file_name(tmpDownFileName); /* 请求文件名 */

			String localFileDir = createTempFileDirectory(Format.Date8Format.parse(tmpDownFileName.substring(7,15))) + File.separator;
			String localFilePath = localFileDir + tmpDownFileName;
			WriteBankLogLn("批次结果文件保存本机路径:" + localFilePath);

			tmpFileCommand.setVs_remote_file_dir(localFileDir); /* 下载本地保存文件路径,完全路径 */
			tmpFileCommand.setVs_remote_file_name(tmpDownFileName); /* 本地文件名 */
			ftpClient.createSocketClient(ftpClient.ipAddress, ftpClient.port);
			boolean isDownloaded = ftpClient.recvFileFromHost(ftpClient.clientSocket, tmpFileCommand);

			String tmpRespCode = "";
			if (isDownloaded) {
				tmpRespCode = "00";
				WriteBankLogLn("批次结果文件下载成功,下次成功保存本机时间:" + Format.DateTimeFormat.format(new Date(new File(localFilePath).lastModified())));
			} else {
				tmpRespCode = "01";
				WriteBankLogLn("批次结果文件下载失败!");
			}
			String tmpNotifyMsg = p_req + tmpRespCode + "|";
			String tmpNotifyMsgLen = String.format("%6s", tmpNotifyMsg.getBytes(getCharset()).length).replace(" ", "0");
			tmpNotifyMsg = "POST816060" + tmpNotifyMsgLen + tmpNotifyMsg;
			WriteBankLogLn("通知返回邮保通响应报文为:\r\n" + tmpNotifyMsg);
			
	        resp.respBytes = tmpNotifyMsg.getBytes(pNtReq.reqCharset);
	    	return resp;
		} catch(Exception ex) {
			WriteBankLogLn(ex);
			throw ex;
		} finally {
			if (ftpClient != null) {
				ftpClient.closeSocketClient(ftpClient.clientSocket);
			}
		}
	}

	@Override
	public void getContentLength(SocketReceiver p_receiver) throws Exception {
		if(p_receiver.contentLength == -1){
			p_receiver.contentLength = p_receiver.readedLength;
			p_receiver.contentStartIndex = 0;
		}
	}
}
